package com.usian.Factory;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * @author chen
 * @date 2021/1/28 - 18:31 - 星期四
 * @project usian_parent
 * @package com.usian.Factory
 * @by GGbomd猪猪侠
 */
@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory {

    //AutowireCapableBeanFactory 可以将一个对象添加到SpringIOC容器中，并且完成该对象注入
    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    /**
     * 解决任务类中注入service报错问题：
     * 	该方法将需要实例化的任务对象手动的添加到springIOC容器中并且完成对象的注入
     */
    public Object createJobInstance(TriggerFiredBundle builder) throws Exception {
        Object object = super.createJobInstance(builder);
        //将obj对象添加Spring IOC容器中，并完成注入
        autowireCapableBeanFactory.autowireBean(object);
        return object;
    }

}
